const { BrowserWindow, app, Tray } = require("electron")
const path = require('path')

app.whenReady().then(() => {
  createTrayWindow()
})

let win, tray
function createTrayWindow() {
  const width = 300
  const height = 420
  win = new BrowserWindow({
    width,
    height,
    frame: false,
    resizable: false,
    show: false,
    movable: false,
    minimizable: false,
    maximizable: false,
  })
  win.loadFile(path.join(__dirname, '../renderer/calculator.html'))
  // 当窗口失去焦点 自动隐藏窗口
  win.on('blur', () => {
    win.hide()
  })
  win.setVisibleOnAllWorkspaces(true)

  const tray = new Tray(path.join(__dirname, '../assets/imgs/calculatorTemplate.png'))
  tray.on('click', () => {
    const trayBounds = tray.getBounds()
    // 让窗口出现在托盘中间正下方
    win.setPosition(trayBounds.x + trayBounds.width / 2 - width / 2, trayBounds.height)
    if (win.isVisible()) {
      win.hide()
    } else {
      win.show()
    }
  })
}